package com.atmilan.quartz.service.impl;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atmilan.quartz.constant.enums.ProductTypeEnum;
import com.atmilan.quartz.model.entity.crmeb.SupplierUnmatchBrand;
import com.atmilan.quartz.service.ISupplierUnmatchBrandService;
import com.atmilan.quartz.utils.unibuy.client.OpenHttp;
import com.atmilan.quartz.utils.unibuy.util.MD5Util;
import com.baomidou.mybatisplus.extension.conditions.query.ChainQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.atmilan.quartz.mapper.StoreBrandMapper;
import com.atmilan.quartz.model.entity.crmeb.StoreBrand;
import com.atmilan.quartz.service.IStoreBrandService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.web.util.UriUtils;

@Slf4j
@Service
public class StoreBrandServiceImpl extends ServiceImpl<StoreBrandMapper, StoreBrand> implements IStoreBrandService {

    /** 百度翻译 **/
    private static final String BDURL = "http://api.fanyi.baidu.com/api/trans/vip/translate";
    private static final String APPID = "20230202001547479";
    private static final String SECRET = "ooWURbYzHCb8hfIijkLE";
    private static final String SALT = "435660288"; // 随机数
//    private static List<Integer> translateIds = Arrays.asList(ProductTypeEnum.GB.getCode(), ProductTypeEnum.BELLETTINI.getCode(),
//            ProductTypeEnum.LEAM.getCode(), ProductTypeEnum.VIETTI.getCode(), ProductTypeEnum.COLTORTI.getCode()
//    );

    @Autowired
    ISupplierUnmatchBrandService supplierUnmatchBrandService;

    @Autowired
    IStoreBrandService storeBrandService;

    @Override
    public StoreBrand getByBrandId(Integer brandId) {
        return this.getOne(Wrappers.<StoreBrand>lambdaQuery().eq(StoreBrand::getId, brandId));
    }

    @Override
    public Map<Integer, String> getBrandNameMap(List<Integer> list) {
        List<StoreBrand> brandList = this.list(Wrappers.<StoreBrand>lambdaQuery().in(StoreBrand::getId, list));
        return brandList.stream().collect(Collectors.toMap(StoreBrand::getId, StoreBrand::getEnName));
    }

    @Override
    public List<String> getBrandNameList() {
        return null;
    }

    @Override
    public void unMatchBrand(StoreBrand buyerStoreBrand, String brandId, String brandName, ProductTypeEnum typeEnum) {
        if(buyerStoreBrand == null){
            int count = supplierUnmatchBrandService.count(Wrappers.<SupplierUnmatchBrand>lambdaQuery()
                    .eq(SupplierUnmatchBrand::getIsDel, 0)
                    .eq(brandId != null, SupplierUnmatchBrand::getOtherId, brandId)
                    .eq(brandName != null, SupplierUnmatchBrand::getEnName, brandName)
                    .eq(SupplierUnmatchBrand::getType, typeEnum.getCode()));
            if(count < 1){
                SupplierUnmatchBrand supplierUnmatchBrand = new SupplierUnmatchBrand();
                supplierUnmatchBrand.setOtherId(brandId);
                supplierUnmatchBrand.setEnName(brandName);
                supplierUnmatchBrand.setType(typeEnum.getCode());
                supplierUnmatchBrandService.save(supplierUnmatchBrand);
            }
        }
    }

    @Override
    public void translateBrand() {

        List<StoreBrand> list = storeBrandService.list(Wrappers.<StoreBrand>lambdaQuery()
                        .gt(StoreBrand::getId,30000)
                .eq(StoreBrand::getStatus, true)
                .eq(StoreBrand::getType, 1)
                .isNull(StoreBrand::getFanyiName)
                .orderByDesc(StoreBrand::getId)
        );


        for (StoreBrand storeBrand : list) {

            if(storeBrand.getEnName().matches("[a-zA-Z]+")){

                log.info("--------------- 是中文进行翻译, name: {} ---------------", storeBrand.getEnName());

                try {
                    String name =storeBrand.getEnName(); // 待翻译内容

                    String nameT = bdTranslate(name);
                    log.info("--------------- nameT: ---------------" + nameT);

                    if(null!=nameT){

                        storeBrand.setFanyiName(nameT);
                        storeBrandService.saveOrUpdate(storeBrand);

                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }





            }else
            {

                log.info("--------------- 是中文不进行翻译, name: {} ---------------", storeBrand.getEnName());


            }





        }













    }

    /**
     * 百度翻译
     * @author daigb
     * @date:  2023-01-30 10:30
     */
    private String bdTranslate(String q) throws IOException {
        if(StringUtils.isBlank(q)) {
            return null;
        }

        String sign = MD5Util.encrypt(APPID + q + SALT + SECRET);
        String q2 = UriUtils.encode(q, "UTF-8");
        String url = BDURL + "?q=" + q2 + "&from=en&to=zh&appid=" + APPID + "&salt=" + SALT + "&sign=" + sign;

        String s = new OpenHttp().get(url, new HashMap<>());
        if(null != s) {
            JSONArray ja = JSONObject.parseObject(s).getJSONArray("trans_result");
            if(null != ja && 0 < ja.size()) {
                String s2 = ja.getJSONObject(0).getString("dst");
                return s2;
            }
        }

        return null;
    }


}
